from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import  ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

#模型
mode = ChatOpenAI(model_name="qwen-plus",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key="sk-4f1498f1c0314ba79ea2919bd7a02c4d",
    temperature=0.7,
    streaming=True
)

#案例一
# for chunk in mode.stream("讲一个隔壁老王的故事"):
#     print(f"故事内容:{chunk.content}")

#案例二
prompt = ChatPromptTemplate.from_template("帮我生成一张草原美景图片:{concept}")
chain  = prompt | mode | StrOutputParser()

for chain in chain.stream({"concept":"关于动物的"}):
    print(chain)